package com.hmys.api.controller;

import com.hmys.api.model.WebAD;
import com.hmys.api.model.WebADPhoto;
import com.hmys.api.response.Result;
import com.hmys.api.response.ResultCode;
import com.hmys.api.service.WebADService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by chen365 on 2018/03/16.
 */
@RestController
@Api(tags = "广告")
public class ADController {

    @Autowired
    private WebADService webADService;

    @ApiOperation(value = "广告", notes = "广告", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "adId", value = "广告ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/a/{adId}")
    public Object detail(@PathVariable("adId") Long adId) {
        if (!StringUtils.isEmpty(adId) && adId < 1) {
            return new Result<>(ResultCode.ERROR_PARAM);
        }
        WebAD webAD = webADService.selectADByPrimaryKey(adId);
        if (webAD != null) {
            if (webAD.getStatus()) {
                List<WebADPhoto> list = webADService.selectAllByAdId(adId);
                webAD.setList(list);
                return new Result<>(ResultCode.SUCCESS, webAD);
            } else {
                return new Result<>(ResultCode.HAVE_DELETED);
            }
        }

        return new Result<>(ResultCode.FAIL);
    }
}
